<template>
  <el-dialog title="导入" :visible.sync="dialogFormVisible" width="30%" :close-on-click-modal="false">
    <el-row>
      <el-col :span="2">所在目录</el-col>
      <el-col :span="10">
        <el-cascader clearable change-on-select @change="directoryIdChange" v-model="directoryId" :options="options" :props="treeProps" :show-all-levels="false" label="label" />
      </el-col>
    </el-row>
    <el-upload
      v-show="isbool"
      v-loading="uploadLoading"
      :data="uploadData"
      :action="uploadImageUrl"
      :show-file-list="false"
      :headers="myHeaders"
      :on-success="handleSuccess"
      :on-error="handleError"
      :before-upload="beforeUpload"
      drag
      :multiple="false"
    >
      <i class="el-icon-upload"></i>
      <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
    </el-upload>
    <div slot="footer" class="dialog-footer">
      <el-button @click=";(dialogFormVisible = false), (directoryId = '')">
        {{ $t('table.cancel') }}
      </el-button>
      <!-- <el-button type="primary" @click="handleTemplate">
        {{ $t('table.template') }}
      </el-button> -->
    </div>
  </el-dialog>
</template>

<script>
import { getToken } from '@/utils/auth'
import request from '@/utils/request'
import { fetchMasterdataDirectoryList } from '@/api/masterdata/masterdataDirectory'

export default {
  name: 'Import',
  props: {
    limit: {
      type: Number,
      required: false,
      default: 10,
    },
    templateUrl: {
      type: String,
      default: undefined,
    },
    importUrl: {
      type: String,
      default: '',
    },
  },
  data() {
    return {
      directoryId: [],
      isbool: false,
      options: [],
      treeProps: {
        value: 'id',
        label: 'name',
      },
      uploadImageUrl: process.env.VUE_APP_BASE_API + '/system/standardsystem/ModelSpecification/import',
      uploadData: {},
      myHeaders: { access_token: getToken() },
      extensions: ['xls', 'xlsx'],
      dialogFormVisible: false,
      uploadLoading: false,
      waringLabel: '只能上传' + this.extension + '文件，且不超过' + this.limit + 'M',
    }
  },
  mounted() {
    fetchMasterdataDirectoryList({
      page: 1,
      limit: 25,
      ancestralId: 2,
    }).then((res) => {
      this.options = res.data.data
    })
  },

  methods: {
    directoryIdChange() {
      this.isbool = true
      this.directoryId = this.directoryId[this.directoryId.length - 1]
      this.uploadData = {
        directoryId: this.directoryId,
      }
    },
    handleTemplate() {
      this.download('/system/standardsystem/ModelSpecification/template', {}, 'post', `${new Date()}.xlsx`)
    },
    show() {
      this.dialogFormVisible = true
    },
    // handleTemplate() {
    //   request({
    //     url: this.templateUrl,
    //     method: 'get',
    //   }).then((response) => {
    //     if (response.data.code === 0) {
    //       import('@/vendor/Export2Excel').then((excel) => {
    //         excel.export_byte_to_excel(response.data.data.bytes, response.data.data.title)
    //         this.downloadLoading = false
    //       })
    //     } else {
    //       this.$message.error(response.data.msg)
    //     }
    //   })
    // },
    handleSuccess(response) {
      if (response.code === 0) {
        this.uploadLoading = false
        this.directoryId = []
        this.$emit('refreshList')
        this.$message.success(response.msg)
      } else {
        this.$message.error(response.msg)
      }
      this.dialogFormVisible = false
    },
    handleError(response) {
      console.log(this.directoryId)
      this.uploadLoading = false
      if (response.msg) {
        this.$message.error(response.msg)
        this.directoryId = ''
      } else {
        this.$message.error('上传失败')
      }
    },
    beforeUpload(file) {
      // 扩展名判断
      const fileExtension = this.getFileExtension(file.name)
      if (fileExtension === '') {
        this.$message.error('文件必须有扩展名')
        return false
      }

      if (this.extensions !== '') {
        const isFileExtension = this.extensions.includes(fileExtension)
        if (!isFileExtension) {
          this.$message.error('上传文件只能是' + this.extensions.join(',') + '格式!')
          return false
        }
      }
      // 判断文件大小
      const isLimitSize = file.size / 1024 / 1024 < this.limit
      if (!isLimitSize) {
        this.$message.error('上传头像图片大小不能超过 ' + this.limit + 'MB!')
      }
      if (isLimitSize) {
        this.uploadLoading = true
      }
      return isLimitSize
    },
    getFileExtension(fileName) {
      if (fileName.lastIndexOf('.') > 0) {
        return fileName.substring(fileName.lastIndexOf('.') + 1)
      }
      return ''
    },
  },
}
</script>

<style lang="scss" scoped>
.el-upload__tip {
  text-align: center;
  color: blue;
  cursor: pointer;
}
</style>
